/*
 * Copyright (C) 2016 Facishare Technology Co., Ltd. All Rights Reserved.
 */
package com.scan.bus.permission;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;


import com.scan.bus.permission.rom.HuaweiUtils;
import com.scan.bus.permission.rom.MeizuUtils;
import com.scan.bus.permission.rom.MiuiUtils;
import com.scan.bus.permission.rom.OppoUtils;
import com.scan.bus.permission.rom.QikuUtils;
import com.scan.bus.permission.rom.RomUtils;

import java.lang.reflect.Method;

/**
 * Description:
 *
 * @author zhaozp
 * @since 2016-10-17
 */

public class VapWindowManager {
    private static final String TAG = "FloatWindowManager";

    private static volatile VapWindowManager instance;
    private WindowManager.LayoutParams mLayout;
    private WindowManager mWindowManager;

    public static VapWindowManager getInstance() {
        if (instance == null) {
            synchronized (VapWindowManager.class) {
                if (instance == null) {
                    instance = new VapWindowManager();
                }
            }
        }
        return instance;
    }

    private VapWindowManager(){
    }


    public boolean checkPermission(Context context) {
        //6.0 版本之后由于 google 增加了对悬浮窗权限的管理，所以方式就统一了
        if (Build.VERSION.SDK_INT < 23) {
            if (RomUtils.checkIsMiuiRom()) {
                return miuiPermissionCheck(context);
            } else if (RomUtils.checkIsMeizuRom()) {
                return meizuPermissionCheck(context);
            } else if (RomUtils.checkIsHuaweiRom()) {
                return huaweiPermissionCheck(context);
            } else if (RomUtils.checkIs360Rom()) {
                return qikuPermissionCheck(context);
            } else if (RomUtils.checkIsOppoRom()) {
                return oppoROMPermissionCheck(context);
            }
        }
        return commonROMPermissionCheck(context);
    }

    private boolean huaweiPermissionCheck(Context context) {
        return HuaweiUtils.checkFloatWindowPermission(context);
    }

    private boolean miuiPermissionCheck(Context context) {
        return MiuiUtils.checkFloatWindowPermission(context);
    }

    private boolean meizuPermissionCheck(Context context) {
        return MeizuUtils.checkFloatWindowPermission(context);
    }

    private boolean qikuPermissionCheck(Context context) {
        return QikuUtils.checkFloatWindowPermission(context);
    }

    private boolean oppoROMPermissionCheck(Context context) {
        return OppoUtils.checkFloatWindowPermission(context);
    }

    private boolean commonROMPermissionCheck(Context context) {
        //最新发现魅族6.0的系统这种方式不好用，天杀的，只有你是奇葩，没办法，单独适配一下
        if (RomUtils.checkIsMeizuRom()) {
            return meizuPermissionCheck(context);
        } else {
            Boolean result = true;
            if (Build.VERSION.SDK_INT >= 23) {
                try {
                    Class clazz = Settings.class;
                    Method canDrawOverlays = clazz.getDeclaredMethod("canDrawOverlays", Context.class);
                    result = (Boolean) canDrawOverlays.invoke(null, context);
                } catch (Exception e) {
                    Log.e(TAG, Log.getStackTraceString(e));
                }
            }
            return result;
        }
    }

    public void createWindowManager(Context context, View view) {
        // 取得系统窗体
        Context applicationContext = context.getApplicationContext();
        if (mWindowManager == null) {
            mWindowManager = (WindowManager) applicationContext.getSystemService(Context.WINDOW_SERVICE);
        }
        if (mLayout == null) {
            // 窗体的布局样式
            mLayout = new WindowManager.LayoutParams();
            // 设置窗体显示类型——TYPE_SYSTEM_ALERT(系统提示)
            //mLayout.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//6.0+
                mLayout.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            } else {
                mLayout.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
            }
            // 设置显示的模式
            mLayout.format = PixelFormat.RGBA_8888;
            // 设置窗体宽度和高度
            DisplayMetrics dm = new DisplayMetrics();
            mWindowManager.getDefaultDisplay().getMetrics(dm);
            mLayout.width = WindowManager.LayoutParams.MATCH_PARENT;
            mLayout.height = WindowManager.LayoutParams.MATCH_PARENT;
            // 设置窗体焦点及触摸：
            mLayout.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
                    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        }

        if (view != null) {
            mWindowManager.addView(view, mLayout);
        }
    }


    public void removeWindow(View view) {
        try {
            if (mWindowManager != null && view != null) {
                mWindowManager.removeViewImmediate(view);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
